﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Net.Mail;
using System.Web.UI;
using System.Data;

namespace PenoyShop.Utilities
{
    public class CodeHelper
    {
        public static int ToInt(object obj)
        {
            int iValue = 0;
            try
            {
                int.TryParse(obj.ToString(), out iValue);
                return iValue;
            }
            catch
            {
                return -1;
            }
        }

        public static decimal ToDecimal(object obj)
        {
            decimal iValue = 0;
            try
            {
                decimal.TryParse(obj.ToString().Trim(), out iValue);
                return iValue;
            }
            catch
            {
                return 0;
            }
        }

        public static DateTime ToDateTime(object obj)
        {
            DateTime dt = DateTime.Now;
            try
            {
                dt = Convert.ToDateTime(obj);
            }
            catch { }
            return dt;
        }

        public static string FormatCurrency(object obj, int decimalPoint)
        {
            decimal result;
            StringBuilder sb = new StringBuilder();
            try
            {
                if (Decimal.TryParse(obj.ToString(), out result))
                {
                    for (int i = 0; i < decimalPoint; i++)
                        sb.Append("#");
                    return String.Format("{0:#,##0." + sb.ToString() + "}", result);
                }
                else
                {
                    return "0";
                }
            }
            catch
            {
                return "0";
            }
        }

        public static string FormatShortDate(object objDate)
        {
            try
            {
                if (objDate != null)
                    return Convert.ToDateTime(objDate).ToString("MM/dd/yyyy");
                else
                    return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string FormatDateFull(object objDate)
        {
            try
            {
                if (objDate != null)
                    return Convert.ToDateTime(objDate).ToString("MM-dd-yyyy hh:mm:ss tt");
                else
                    return string.Empty;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Get total page
        /// </summary>
        /// <param name="TotalRow">TotalRow</param>
        /// <param name="PageSize">PageSize</param>
        /// <returns>int</returns>
        public static int GetTotalPage(int TotalRow, int PageSize)
        {
            return TotalRow == 0 ? 0 : TotalRow / PageSize + (TotalRow % PageSize > 0 ? 1 : 0);
        }

        public static string Sendmail(string passEmail, string Body, bool IsAffiliate)
        {
            //string sTitle = ddlTitle.SelectedItem.Text;
            //string sBody = txtQuestion.Text;

            //try
            //{
            //    #region Read file template
            //    string sFileName = Server.MapPath(@"~/Template/MailQuestion.html");
            //    StreamReader sR = File.OpenText(sFileName);
            //    string sTemplate = sR.ReadToEnd();
            //    #endregion

            //    #region Send mail



            //    XmlDocument doc = new XmlDocument();
            //    string path = Server.MapPath(@"~/XML/MailConfig.xml");
            //    doc.Load(path);
            //    XmlNodeList xnList = doc.SelectNodes("ConfigList/Config");

            //    string Email = string.Empty;
            //    string Password = string.Empty;
            //    string Title = string.Empty;
            //    string DisplayName = string.Empty;
            //    string To = string.Empty;
            //    string port = string.Empty;
            //    string smtp = string.Empty;
            //    foreach (XmlNode xn in xnList)
            //    {
            //        Email = xn["From"].InnerText;
            //        To = xn["To"].InnerText;
            //        Password = xn["Password"].InnerText;
            //        Title = xn["Title"].InnerText;
            //        DisplayName = xn["DisplayName"].InnerText;
            //        port = xn["Port"].InnerText;
            //        smtp = xn["SMTP"].InnerText;
            //    }


            //    sTemplate = sTemplate.Replace("[SUBJECT]", sTitle);
            //    sTemplate = sTemplate.Replace("[BODY]", sBody);

            //    using (MailMessage mail = new MailMessage())
            //    {
            //        mail.From = new MailAddress(Email, "New Paradise");
            //        mail.To.Add(To);
            //        mail.CC.Add("vucongtam@ymail.com");
            //        mail.Subject = "Question from client";
            //        mail.Body = sTemplate;
            //        mail.IsBodyHtml = true;
            //        mail.SubjectEncoding = System.Text.Encoding.UTF8;
            //        mail.BodyEncoding = System.Text.Encoding.UTF8;
            //        //mail.Attachments.Add(new Attachment(Server.MapPath("MailTemplate.htm")));
            //        SmtpClient client = new SmtpClient(smtp, Convert.ToInt32(port));
            //        client.Credentials = new System.Net.NetworkCredential(Email, Password);
            //        client.EnableSsl = true;
            //        client.Send(mail);
            //    }

            //    #endregion
            //}
            //catch (Exception ex)
            //{
            //    Logger.FrameworkLogger.Error("Send Email ", ex);
            //}

            return string.Empty;
        }

        public static string FormatNumeric(object obj, int places)
        {
            string result = string.Empty;
            StringBuilder sb = new StringBuilder();
            try
            {
                string dec = "0:0.";
                string dec2 = "0.";

                for (int i = 0; i < places; i++)
                {
                    dec += "0";
                    dec2 += "0";
                }
                result = String.Format("{" + dec + "}", obj);
                result = Convert.ToDouble(result).ToString("###,###,##" + dec2);
            }
            catch
            {
                result = "-1";
            }
            return result;
        }

        #region Add Alert function
        public static void Alert(string message, string url)
        {
            try
            {
                string cleanMessage = message.Replace("'", "\'");
                string script1 = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
                string script2 = "<script type=\"text/javascript\">alert('" + cleanMessage + "'); window.location.href='" + url + "'</script>";
                string script = url == "" ? script1 : script2;
                Page page = HttpContext.Current.CurrentHandler as Page;
                if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
                {
                    page.ClientScript.RegisterClientScriptBlock(typeof(object), "alert", script);
                }
            }
            catch { }
        }
        #endregion

     

        public static string GetKeySetting(string Key)
        {
            XmlDocument doc = new XmlDocument();
            string path = HttpContext.Current.Server.MapPath(@"~/XML/Config.xml");
            doc.Load(path);
            XmlNodeList xnList = doc.SelectNodes("Setting");
            DataSet ds = new DataSet();
            ds.ReadXml(path);
            DataTable dt = ds.Tables[0];
            string sResult = dt.Rows[0][Key].ToString();
            return sResult;
        }
    }
}
